package com.xqls.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.CompanyBean;
import com.xqls.biz.OperationRecordBiz;
import com.xqls.service.CompanyService;
import com.xqls.service.OperationRecordService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.DownloadAct;
import com.xqls.util.VelocityUtil;

@Service
public class OperationRecordBizImpl implements OperationRecordBiz {
	private static Logger logger = Logger.getLogger(OperationRecordBiz.class);
	@Autowired
	private OperationRecordService opRdservice;
	@Autowired
	private CompanyService companyService;

	@Override
	public CommonResult getOperationRecordsByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		try {
			// 查询企业名称
			Map<String, Object> page = commonPage.pageToMap();
			CommonResult commonResult = companyService.queryByDeviceNo((String) page.get("enterpriseNo"));
			CompanyBean companyBean = (CompanyBean) commonResult.getData();
			List<Map<String,Object>> opList=opRdservice.queryByMap(commonPage.pageToMap());
			for (int i=0;i<opList.size();i++){
				String st=opList.get(i).get("recordContent").toString();
				String[] sq=st.split("，");
				String[] sx=new String[50];
				String sd="";
				int k=0;
				for (int j = 0; j < sq.length; j++) {
					if(sq[j].indexOf("null")!=-1)
					{

					}else{
						sx[k]=sq[j];
						k++;
					}
				}
				for(int l=0;l<k;l++){

					if(l!=k-1){
						sd=sd+sx[l];
						sd+="，";
					}else {
						sd=sd+sx[l];
					}
				}
				opList.get(i).put("recordContent",sd);
			}
			commonPage.setPageData(opList);
			@SuppressWarnings("unchecked")
			List<Map<String, Object>> listMap = (List<Map<String, Object>>) commonPage.getPageData();
			if (commonPage.curr > 1) {
				int j = (commonPage.curr - 1) * (commonPage.pageSize);
				for (int i = j; i < (listMap.size() + j); i++) {
					listMap.get(i - j).put("no", i + 1);
				}
			} else {
				for (int i = 0; i < listMap.size(); i++) {
					listMap.get(i).put("no", i + 1);
				}
			}
			Map<String, Object> showMap = new HashMap<String, Object>();
			showMap.put("companyName", companyBean.getCompanyName());
			showMap.put("operationList", listMap);
			commonPage.setHtml(VelocityUtil.getHtmlByMap("operationRecord/operationRecordListData.vm", showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("分页条件查询操作记录异常", e);
		}
		return result;
	}

	@Override
	public void getExcelByMap(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		try {
			List<Map<String, Object>> recordList = this.opRdservice.getListByMap(map);
			if(recordList.isEmpty())
			{
				
			}
			else
			{
				String sheetName=DictionaryEnums.find(Integer.parseInt(map.get("urlModularNo").toString())).getMsg()+"操作记录";
				Set<String> keySet = recordList.get(0).keySet();
				String []keys=keySet.toArray(new String[keySet.size()]);
				String columnNames[]={"序号","企业","记录时间","记录内容"};
				DownloadAct.excelDownload(request, response, recordList, columnNames, keys, sheetName);
			}
		} catch (Exception e) {
			logger.error("操作记录导出异常", e);
		}
	}
	
	
	public CommonResult save(String enterpriseNo, int modularNo, String operationType, String recordContent,
			String operationSource) {
		CommonResult commonResult=new CommonResult();
		boolean success=false;
		try {
			success=opRdservice.save(enterpriseNo, modularNo, operationType, recordContent, operationSource);
		} catch (Exception e) {
			logger.error("操作记录保存异常", e);
		}
		commonResult.setIsSuccess(success);
		return  commonResult;
	}
}
